using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VFBST
    {
        public DataRow Convert2FBST(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryFBST(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["FBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["FBART"] = " ".PadRight(20 ); 
         dwReturn["FBVAR"] = " ".PadRight(2 ); 
         dwReturn["FBFARBE"] = " ".PadRight(10 ); 
         dwReturn["FBGROE"] = " ".PadRight(7 ); 
         dwReturn["FBAUFM"] = " ".PadRight(2 ); 
         dwReturn["FBVPEINH"] = " ".PadRight(3 ); 
         dwReturn["FBANZVP"] = " ".PadRight(4 ); 
         dwReturn["FBZS"] = " ".PadRight(2 ); 
         dwReturn["FBCHAR"] = " ".PadRight(7 ); 
         dwReturn["FBGNR"] = " ".PadRight(4 ); 
         dwReturn["FBGNRPOS"] = " ".PadRight(4 ); 
         dwReturn["FBLO"] = " ".PadRight(2 ); 
         dwReturn["FBLB"] = " ".PadRight(2 ); 
         dwReturn["FBLP"] = " ".PadRight(8 ); 
         dwReturn["FBME"] = " ".PadRight(0 ); 
         dwReturn["FBZUME"] = " ".PadRight(0 ); 
         dwReturn["FBAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["FBCHARL"] = " ".PadRight(15 ); 
         dwReturn["FBCHARLZUS"] = " ".PadRight(15 ); 
         dwReturn["FBSPKZ"] = " ".PadRight(1 ); 
         dwReturn["FBSPSTA"] = " ".PadRight(2 ); 
         dwReturn["FBSPGRD"] = " ".PadRight(2 ); 
         dwReturn["FBFILLER"] = " ".PadRight(75 ); 
         dwReturn["FBAENDZL"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapFBST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveFBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryFBST(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapFBST(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertFBST(dwLocal, ITXDBVERSION);
            }
            UpdateFBST(dwLocal);
        }
        public void InsertFBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO FBST(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryFBST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM FBST WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM FBST  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "FBST";
            Return = Result;
            return Return;
        }
        public void DeleteFBST(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM FBST   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateFBST(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE FBST SET "
                    +"FBMANDANT= :FBMANDANT,"
                    +"FBART= :FBART,"
                    +"FBVAR= :FBVAR,"
                    +"FBFARBE= :FBFARBE,"
                    +"FBGROE= :FBGROE,"
                    +"FBAUFM= :FBAUFM,"
                    +"FBVPEINH= :FBVPEINH,"
                    +"FBANZVP= :FBANZVP,"
                    +"FBZS= :FBZS,"
                    +"FBCHAR= :FBCHAR,"
                    +"FBGNR= :FBGNR,"
                    +"FBGNRPOS= :FBGNRPOS,"
                    +"FBLO= :FBLO,"
                    +"FBLB= :FBLB,"
                    +"FBLP= :FBLP,"
                    +"FBME= :FBME,"
                    +"FBZUME= :FBZUME,"
                    +"FBAUFBKZ= :FBAUFBKZ,"
                    +"FBCHARL= :FBCHARL,"
                    +"FBCHARLZUS= :FBCHARLZUS,"
                    +"FBSPKZ= :FBSPKZ,"
                    +"FBSPSTA= :FBSPSTA,"
                    +"FBSPGRD= :FBSPGRD,"
                    +"FBFILLER= :FBFILLER,"
                    +"FBAENDZL= :FBAENDZL,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("FBMANDANT", OracleType.NVarChar).Value = dwData["FBMANDANT"].ToString(); 
          or1.Parameters.Add("FBART", OracleType.NVarChar).Value = dwData["FBART"].ToString(); 
          or1.Parameters.Add("FBVAR", OracleType.NVarChar).Value = dwData["FBVAR"].ToString(); 
          or1.Parameters.Add("FBFARBE", OracleType.NVarChar).Value = dwData["FBFARBE"].ToString(); 
          or1.Parameters.Add("FBGROE", OracleType.NVarChar).Value = dwData["FBGROE"].ToString(); 
          or1.Parameters.Add("FBAUFM", OracleType.NVarChar).Value = dwData["FBAUFM"].ToString(); 
          or1.Parameters.Add("FBVPEINH", OracleType.NVarChar).Value = dwData["FBVPEINH"].ToString(); 
          or1.Parameters.Add("FBANZVP", OracleType.NVarChar).Value = dwData["FBANZVP"].ToString(); 
          or1.Parameters.Add("FBZS", OracleType.NVarChar).Value = dwData["FBZS"].ToString(); 
          or1.Parameters.Add("FBCHAR", OracleType.NVarChar).Value = dwData["FBCHAR"].ToString(); 
          or1.Parameters.Add("FBGNR", OracleType.NVarChar).Value = dwData["FBGNR"].ToString(); 
          or1.Parameters.Add("FBGNRPOS", OracleType.NVarChar).Value = dwData["FBGNRPOS"].ToString(); 
          or1.Parameters.Add("FBLO", OracleType.NVarChar).Value = dwData["FBLO"].ToString(); 
          or1.Parameters.Add("FBLB", OracleType.NVarChar).Value = dwData["FBLB"].ToString(); 
          or1.Parameters.Add("FBLP", OracleType.NVarChar).Value = dwData["FBLP"].ToString(); 
          or1.Parameters.Add("FBME", OracleType.NVarChar).Value = dwData["FBME"].ToString(); 
          or1.Parameters.Add("FBZUME", OracleType.NVarChar).Value = dwData["FBZUME"].ToString(); 
          or1.Parameters.Add("FBAUFBKZ", OracleType.NVarChar).Value = dwData["FBAUFBKZ"].ToString(); 
          or1.Parameters.Add("FBCHARL", OracleType.NVarChar).Value = dwData["FBCHARL"].ToString(); 
          or1.Parameters.Add("FBCHARLZUS", OracleType.NVarChar).Value = dwData["FBCHARLZUS"].ToString(); 
          or1.Parameters.Add("FBSPKZ", OracleType.NVarChar).Value = dwData["FBSPKZ"].ToString(); 
          or1.Parameters.Add("FBSPSTA", OracleType.NVarChar).Value = dwData["FBSPSTA"].ToString(); 
          or1.Parameters.Add("FBSPGRD", OracleType.NVarChar).Value = dwData["FBSPGRD"].ToString(); 
          or1.Parameters.Add("FBFILLER", OracleType.NVarChar).Value = dwData["FBFILLER"].ToString(); 
          or1.Parameters.Add("FBAENDZL", OracleType.NVarChar).Value = dwData["FBAENDZL"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
